<?php

namespace App\Http\Controllers\Client\Shop;

use App\Http\Controllers\ClientController;
use App\Models\Common\File;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ShopBindController extends ClientController
{
    /**
     * 绑定店铺(新增/修改)
     */

    public function index()
    {

        $all = $this->all;

        try {
            $shop_message = [
                'we_id' => $this->we_id,
                'shop_name' => $all['shop_name'],
                'key_word' => $all['key_word'],
                'contact_man' => $all['contact_man'],
                'contact_phone' => $all['contact_phone'],
                'verified' => 0, // 待审核
                'link_url' => $all['link_url'],
                'province' => $all['address'][0],
                'city' => $all['address'][1],
                'district' => $all['address'][2],
                'address' => $all['address_detail'],
            ];
            if (empty($all['shop_id']) && empty($all['head_img'])) {
                throw new \Exception('参数错误');
            }
            if ($all['head_img']) {
                $shop_message['head_img'] = File::saveMedia($all['head_img'], 'images/shop');
            }
            if (isset($all['shop_id'])) {
                // 修改店铺
                DB::table('shop')->where('shop_id', '=', $all['shop_id'])->update($shop_message);
            } else {
                // 新增店铺
                $shop_message['shop_type'] = $all['shop_type']; // 店铺类型不允许修改
                DB::table('shop')->insert($shop_message);
            }
            return $this->setReturnJson(0, '店铺信息提交成功，请等待工作人员审核通过后，您便可以发单。');
        } catch (\Exception $e) {
            Log::error(ShopBindController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '绑定失败');
        }
    }
}
